package common.util;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import com.zdxc.entities.Userinfo;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


import common.constant.GlobalConstant;

/**
 * 用于从HttpServletRequest和HttpSession中获取用户的各种信息
 * 
 * @author liuxj
 * @version 1.0.0 2015年1月5日
 */
public class UserInfoUtils {
    /**
     * 获取用户请求
     * 
     * @return HttpServletRequest
     */
    public static HttpServletRequest getRequest() {
        HttpServletRequest request = null;

        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (servletRequestAttributes != null) {
            request = servletRequestAttributes.getRequest();
        }

        return request;
    }

    /**
     * 获取用户会话
     * 
     * @return HttpSession
     */
    public static HttpSession getSession() {
        HttpSession session = null;

        HttpServletRequest request = getRequest();
        if (request != null) {
            session = request.getSession();
        }

        return session;
    }

    /**
     * 获取用户SessionId
     * 
     * @return SessionId
     */
    public static String getSessionId() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();

        if (requestAttributes != null) {
            return requestAttributes.getSessionId();
        }

        return null;
    }

    /**
     * 获得保存在Session中的User
     * 
     * @return User
     */
    public static Userinfo getUser() {
        HttpSession session = getSession();

        if (session == null) {
            return null;
        }

        try {
            return (Userinfo) getSession().getAttribute(GlobalConstant.LOGIN_USER_KEY);
        } catch (Exception e) {
        }

        return null;
    }

    /**
     * 获得登录用户ID
     * 
     * @return 用户的ID
     */
    public static String getUserId() {
        Userinfo User = getUser();
        if (User != null) {
            return User.getUserId().toString();
        }

        return null;
    }



    /**
     * 获得登录用户ID
     *
     * @return 用户的ID
     */
    public static String getFullName() {
        Userinfo User = getUser();
        if (User != null) {
            return User.getNickname();
        }

        return null;
    }

    /**
     * 获得登录用户的用户名
     * 
     * @return 用户名
     */
    public static String getUsername() {
        if (getUser() != null) {
            return getUser().getUserName();
        }
        
        return null;
    }



    /**
     * 获得访问用户的IP地址
     * 
     * @return IP
     */
    public static String getUserIp() {
        HttpServletRequest request = getRequest();

        if (request == null) {
            return null;
        }

        String ipAddress = null;
        ipAddress = getRequest().getHeader("x-forwarded-for");
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = getRequest().getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = getRequest().getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = getRequest().getRemoteAddr();

            if ("127.0.0.1".equals(ipAddress)) {
                // 根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }

                ipAddress = inet.getHostAddress();
            }

        }
        // 对于通过多个代理的情况，第一个IP为客户端真实IP，多个IP按照','分割
        if (ipAddress != null && ipAddress.length() > 15) {
            if (ipAddress.indexOf(",") > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
            }
        }

        return ipAddress;
    }

    /**
     * 向Session中设置登录用户的用户信息
     * 
     * @param User
     */
    public static void setUser(Userinfo User) {
        HttpSession session = getSession();

        if (session != null) {
            session.setAttribute(GlobalConstant.LOGIN_USER_KEY, User);
        }
    }

    /**
     * 判断用户是否已经登录
     * 
     * @return true:用户已登录；false:用户没有登录
     */
    public static boolean userLogined() {
        if (getUser() != null) {
            return true;
        }

        return false;
    }

}
